<script setup lang="ts">
  import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
  import { FormInstance } from 'element-plus'
  import { reqForceLogout, reqOnlinelist } from '@/api/monitor/online'
  import { reqTableData } from '@/api/type'
  import { parseTime } from '@/utils'
  import { reqDelNotice, reqListNotice } from '@/api/system/notice'
  import DictTag from '@/components/DictTags/index.vue'
  import NoticeDialog from '@/views/system/notice/components/noticeDialog.vue'

  const { proxy }: any = getCurrentInstance()
  const total = ref(0)
  const loading = ref(true)
  const tableData = ref([])
  const noticeDialogRef = ref()
  const ruleFormRef = ref<FormInstance>()
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    noticeType: undefined,
    status: undefined,
  })
  const single = ref(true)
  const nodeIds = ref([])
  const multiple = ref(true)

  const getList = async () => {
    loading.value = true
    await reqListNotice(queryParams).then((req: reqTableData) => {
      if (req.code === 200) {
        tableData.value = req.rows
        total.value = req.total
      } else {
        proxy.$modal.msgError('获取列表失败！')
      }
    })
    setTimeout(() => {
      loading.value = false
    }, 500)
  }

  // 搜索
  const handleQuery = () => {
    queryParams.pageNum = 1
    getList()
  }

  // 重置
  const resetQuery = () => {
    ruleFormRef.value.resetFields()
    Object.keys(queryParams).forEach((key) => {
      if (key === 'pageNum') {
        queryParams[key] = 1
      } else if (key === 'pageSize') {
        queryParams[key] = 10
      } else {
        queryParams[key] = undefined
      }
    })
    handleQuery()
  }

  // 新增
  const handleAdd = () => {
    noticeDialogRef.value.show()
  }

  // 修改
  const handleUpdate = (row: any) => {
    noticeDialogRef.value.show(row)
  }

  const handleDelete = (row) => {
    const noticeIds = row.noticeId || nodeIds.value
    proxy.$modal
      .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
      .then(function () {
        return reqDelNotice(noticeIds)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
      })
      .catch(() => {})
  }

  /*多选框*/
  const handleSelectionChange = (selection: any) => {
    nodeIds.value = selection.map((item: any) => item.noticeId)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  onMounted(() => {
    handleQuery()
  })
</script>

<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="queryParams" ref="ruleFormRef" label-width="68px">
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input
            v-model="queryParams.noticeTitle"
            placeholder="请输入公告标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作人员" prop="createBy">
          <el-input
            v-model="queryParams.createBy"
            placeholder="请输入操作人员"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="类型" prop="noticeType">
          <el-select v-model="queryParams.noticeType" placeholder="公告类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_notice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <div class="util">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增
        </el-button>
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
          >删除
        </el-button>
      </div>
      <div class="table-inner">
        <el-table
          v-loading="loading"
          :data="tableData"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          border
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="noticeId" width="100" />
          <el-table-column
            label="公告标题"
            align="center"
            prop="noticeTitle"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="公告类型" align="center" prop="noticeType" width="100">
            <template #default="scope">
              <DictTag :options="dict.type.sys_notice_type" :value="scope.row.noticeType" />
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status" width="100">
            <template #default="scope">
              <DictTag :options="dict.type.sys_notice_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建者" align="center" prop="createBy" width="100" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="110">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            v-hasPermi="['system:notice:edit', 'system:notice:remove']"
          >
            <template #default="scope">
              <el-button
                size="small"
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:notice:edit']"
                >修改</el-button
              >
              <el-button
                size="small"
                type="warning"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:notice:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <!--分页器            -->
        <el-pagination
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          @current-change="getList"
          @size-change="getList"
        />
      </div>
    </div>
    <NoticeDialog ref="noticeDialogRef" @getList="getList"></NoticeDialog>
  </div>
</template>

<script lang="ts">
  export default {
    dicts: ['sys_notice_status', 'sys_notice_type'],
  }
</script>

<style scoped lang="scss">
  @import './index';
</style>
